package com.wac.data.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wac.data.entity.Criticism;
import com.wac.data.service.CriticismService;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/criticism")
public class CriticismController {
	private static ObjectMapper objectMapper = new ObjectMapper();


	@Resource
	private CriticismService criticismService;
	@Resource
	private UserService userService;

	@RequestMapping("/list")
	public String getEntityCriticismList(int page, String id, int order, HttpServletRequest request) throws JsonProcessingException {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		return objectMapper.writeValueAsString(criticismService.selectByBangumiId(page, id, order, userId));
	}

	@RequestMapping("/user")
	public String getUserCriticismList(int page, String id) throws JsonProcessingException {
		return objectMapper.writeValueAsString(criticismService.selectByUserId(page, id));
	}

	@RequestMapping("/add")
	public String addCriticism(@RequestBody Criticism criticism, HttpServletRequest request) {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		criticism.setUserId(userId);
		if (criticismService.addCriticism(criticism) == 1) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/delete")
	public String addCriticism(String id) {
		if (criticismService.deleteCriticism(id) == 1) {
			return "1";
		}
		return "0";
	}
}

